package com.sias.realestate.web.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sias.realestate.web.model.Notification;
import com.sias.realestate.web.model.Owner;
import com.sias.realestate.web.model.User;
import com.sias.realestate.web.service.NotificationService;
import com.sias.realestate.web.service.OwnerService;
import com.sias.realestate.web.service.UserService;
import com.sias.realestate.web.util.FinalVarUtil;
import com.sias.realstate.web.pagemodel.PageModel;

@Controller
@RequestMapping(value = "/notification")
public class NotificationController {

	@Resource
	private NotificationService notificationService;
	@Resource
	private OwnerService ownerService;
	@Resource
	private UserService userService;

	@RequestMapping(value = "/getAllNotification", method = RequestMethod.GET)
	public ModelAndView getAllNotification(HttpServletRequest request)
			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		List<Notification> list = notificationService.selectList();
		String th_list = FinalVarUtil.notificationTh;
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("notification/notificationList");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：发布通知jsp 创建日期：2016年12月14日上午10:09:46
	 */
	@RequestMapping(value = "/addNotificationJsp", method = RequestMethod.GET)
	public ModelAndView addNotificationJsp(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("notification/addnotification");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：发布通知 创建日期：2016年12月14日上午11:29:20
	 */
	@RequestMapping(value = "/addNotification", method = RequestMethod.POST)
	public ModelAndView addNotification(HttpServletRequest request, Integer type, String content) {
		ModelAndView mv = new ModelAndView();
		Notification notification = new Notification();
		notification.setnType(type);
		notification.setoId(0);
		notification.setnTime(new Date());
		notification.setnContent(content);
		notificationService.insert(notification);
		request.setAttribute("successInfo", "发布通知成功！");
		request.setAttribute("page_code", "allnotification");
		mv.setViewName("/success-info");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：撤销通知 创建日期：2016年12月14日上午10:09:46
	 */
	@RequestMapping(value = "/cancelNotification", method = RequestMethod.GET)
	public ModelAndView cancelNotification(HttpServletRequest request, Integer nid) {
		notificationService.delete(nid);
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		List<Notification> list = notificationService.selectList();
		String th_list = FinalVarUtil.notificationTh;
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("notification/notificationList");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：进入修改通知Jsp 创建日期：2016年12月14日上午10:09:46
	 */
	@RequestMapping(value = "/editNotificationJsp", method = RequestMethod.GET)
	public ModelAndView editNotificationJsp(HttpServletRequest request, Integer nid) {
		ModelAndView mv = new ModelAndView();
		Notification notification = notificationService.selectById(nid);
		mv.addObject("notification", notification);
		mv.setViewName("notification/editnotification");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：修改通知 创建日期：2016年12月14日上午10:09:46
	 */
	@RequestMapping(value = "/editNotification", method = RequestMethod.POST)
	public ModelAndView editNotification(HttpServletRequest request, Integer nid, Integer type, String content) {
		ModelAndView mv = new ModelAndView();
		Notification notification = notificationService.selectById(nid);
		notification.setnType(type);
		notification.setoId(0);
		notification.setnTime(new Date());
		notification.setnContent(content);
		notificationService.update(notification);
		request.setAttribute("successInfo", "修改通知成功！");
		request.setAttribute("page_code", "allnotification");
		mv.setViewName("/success-info");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：我收到的通知 创建日期：2016年12月13日下午4:35:37
	 */
	@RequestMapping(value = "/getMyNotification", method = RequestMethod.GET)
	public ModelAndView getMyNotification(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		User user = (User) request.getSession().getAttribute("user");
		Owner owner = ownerService.findByIdcard(user.getuIdcard());
		List<Notification> list = null;
		if (owner != null) {
			list = notificationService.findNotificationByOid(owner.getoId().toString());// 根据oid查询我收到的通知
		}
		String th_list = FinalVarUtil.mynotificationTh; // 获取我收到的通知的表头
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("notification/mynotificationList");
		return mv;
	}

}
